package com.linksfield.topup.service.impl;

import com.linksfield.topup.mapper.SequenceMapper;
import com.linksfield.topup.service.SequenceService;
import com.linksfield.topup.utils.sequence.DateUtils;
import com.linksfield.topup.utils.sequence.SequenceHepler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 序列业务逻辑处理实现类
 *
 * @author: lm
 * @date: 2019-12-02 15:40
 */

@Transactional(rollbackFor = Exception.class)

@Service("sequenceService")

public class SequenceServiceImpl implements SequenceService {

    @Resource
    SequenceMapper sequenceMapper;

    private String SERVICEPLANCODE_SEQ = "seq_sevicePlanCode_connseqno";

    private String PLANCODE_SEQ = "seq_planCode_connseqno";

    /**
     * 生成序列值大方法
     *
     * @return
     */

    @Override
    public String getSevicePlanCodeNo() {

        String timeFix = DateUtils.format(new Date(), "yyyyMMdd");
        return SequenceHepler.generateSequence(
                /** 前缀 */
                String.format("%s%s", "SP", timeFix),
                /** 当前序号值 */
                sequenceMapper.findCurrentVal(SERVICEPLANCODE_SEQ),
                /** 数值长度 */
                4);
    }

    @Override
    public String getPlanCodeNo() {
        String timeFix = DateUtils.format(new Date(), "yyyyMMdd");
        return SequenceHepler.generateSequence(
                /** 前缀 */
                String.format("%s%s", "PL", timeFix),
                /** 当前序号值 */
                sequenceMapper.findCurrentVal(PLANCODE_SEQ),
                /** 数值长度 */
                4);
    }

}
